﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Data.SqlClient;
using System.Text;


namespace FinalWeb
{
    public partial class UserLogin : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Session["UserName"] = txtZhang.Text;

        }

        protected void btnRe_Click(object sender, EventArgs e)
        {
            this.txtZhang.Text = "";
            this.txtPwd.Text = "";
            this.TextBox1.Text = "";

        }

        protected void Login_Click(object sender, EventArgs e)
        {


            if (txtZhang.Text == "")
            {
                Response.Write(@"<script>alert('用户名不能为空！');</script>");
            }
            if (txtPwd.Text == "")
            {
                Response.Write(@"<script>alert('密码不能为空！');</script>");
            }
            if (TextBox1.Text == "")
            {
                Response.Write(@"<script>alert('验证码不能为空！');</script>");
            }
            string strCn = "server=pc.hstc.cn;database=dhd2014112102;uid=2014112102;password=19960523";

            SqlConnection cn = new SqlConnection(strCn);
            try
            {
                cn.Open();
                string strSql = "SELECT * FROM UserInfo where UserName =N'" + this.txtZhang.Text + "'";
                SqlCommand cmd = new SqlCommand(strSql, cn);
                SqlDataReader c = cmd.ExecuteReader();
                string password = "";
                if (Session["CheckCode"] != null)
                {
                    string checkcode = Session["CheckCode"].ToString();
                    if (c.Read())
                    {
                        password = (string)c.GetValue(1);
                        if (password == txtPwd.Text && this.TextBox1.Text == checkcode)
                        {
                            Response.Write(@"<script language=javascript>alert('登陆成功！');window.window.location.href='UserFirst.aspx';</script>");

                        }
                        else
                        {
                            Response.Write(@"<script>alert('信息出现错误！请重新输入');</script>");
                        }

                    }
                }
                else
                {
                    Response.Write(@"<script>alert('该用户名不存在！');</script>");
                }
                c.Close();

            }

            finally
            {
                cn.Close();
            }


        }

        protected void btnReturn_Click(object sender, EventArgs e)
        {
            Response.Redirect("index.aspx");
        }
    }
}